package com.lnj.orders.utils;

import com.lnj.bean.dto.AddressDTO;
import com.lnj.bean.model.Address;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 地址实体和DTO转换工具
 */
public class AddressConvertor {

    public static Address toEntity(AddressDTO dto) {
        if (dto == null) {
            return null;
        }
        Address entity = new Address();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }

    public static AddressDTO toDto(Address entity) {
        if (entity == null) {
            return null;
        }
        AddressDTO dto = new AddressDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    public static List<AddressDTO> toDtoList(List<Address> entities) {
        if (entities == null) {
            return null;
        }
        return entities.stream().map(AddressConvertor::toDto).collect(Collectors.toList());
    }
}
